package top.dancun.easuser.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.dancun.common.entity.RoleEntity;
import top.dancun.common.utils.PageUtils;
import top.dancun.common.utils.R;
import top.dancun.easuser.service.RoleService;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;



/**
 * 
 *
 * @author dancun
 * @email message@dancun.top
 * @date 2022-09-05 22:15:48
 */
@RestController
@RequestMapping("/role")
public class RoleController extends AbstractController{
    @Autowired
    private RoleService roleService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("easuser:role:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = roleService.queryPage(params);

        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("easuser:role:info")
    public R info(@PathVariable("roleId") Long roleId){
		RoleEntity role = roleService.selectById(roleId);


        return R.ok().put("role", role);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("easuser:role:save")
    public R save(@RequestBody RoleEntity role){
        role.setCreateTime(new Date());
		roleService.saveRoleAndMenu(role);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("easuser:role:update")
    public R update(@RequestBody RoleEntity role){
		roleService.updateRoleAndMenu(role);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("easuser:role:delete")
    public R delete(@RequestBody RoleEntity role){
		roleService.deleteRoleAndMenu(role);

        return R.ok();
    }

    /**
     * 批量删除
     */
    @RequestMapping("/deleteall")
    @RequiresPermissions("easuser:role:delete")
    public R deleteAll(@RequestBody Long[] roleIds){
        roleService.deleteAllRole(Arrays.asList(roleIds));

        return R.ok();
    }

}
